﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FortuneHeavenCommon.DataMap;
using System.Data.SqlClient;

namespace FortuneHeavan.Game.DAL
{
    public class animalrecordDAL : TreasureDBBase
    {
        public List<animalrecord> GetGroupList(int page, int size, string id)
        {
            if (string.IsNullOrEmpty(id) || id.Trim() == "")
            {
                string sql = @"select * from (
select RoomID,RoomName,TableID,Sum(AnimalSysScore) as animalscore,Sum(EnjonyGameSysScore) as enjoygamescore , ROW_NUMBER() OVER(ORDER BY RoomID DESC) as ROW
from animalrecord 
group by RoomID,RoomName,TableID  ) FF
 WHERE FF.ROW BETWEEN  @p0 AND @p1";
                List<animalrecord> list = Query<animalrecord>(sql, new SqlParameter("@p0", size * (page - 1) + 1), new SqlParameter("@p1", size * page));
                return list;
            }
            else
            {
                string sql = @"select * from (
select RoomID,RoomName,TableID,Sum(AnimalSysScore) as animalscore,Sum(EnjonyGameSysScore) as enjoygamescore , ROW_NUMBER() OVER(ORDER BY RoomID DESC) as ROW
from animalrecord where RoomID  in (" + id + @")
group by RoomID,RoomName,TableID  ) FF
 WHERE FF.ROW BETWEEN  @p0 AND @p1";
                List<animalrecord> list = Query<animalrecord>(sql, new SqlParameter("@p0", size * (page - 1) + 1), new SqlParameter("@p1", size * page));
                return list;
            }
        }
        public int GetGroupTotalSize(string id)
        {
            if (!string.IsNullOrEmpty(id) && id.Trim() != "")
            {
                string sql = @"select COUNT (RoomID) from 
(select  RoomID from animalrecord where RoomID in (" + id + @")  group by RoomID,RoomName,TableID) ff";
                return RunSQL(sql);
            }
            else
            {
                string sql = @"select COUNT (RoomID) from 
(select  RoomID from animalrecord   group by RoomID,RoomName,TableID) ff";
                return RunSQL(sql);
            }
        }


        public List<animalrecord> GetList(int page, int size, string roomid, string tableid)
        {

            string condition = "where 1=1 ";
            List<SqlParameter> content = new List<SqlParameter>();
            if (!string.IsNullOrEmpty(roomid))
            {
                condition += " and RoomID =@roomid";
                content.Add(new SqlParameter("@roomid", roomid));
            }
            if (!string.IsNullOrEmpty(tableid))
            {
                condition += " and TableID =@tableid";
                content.Add(new SqlParameter("@tableid", tableid));
            }
            content.Add(new SqlParameter("@p0", size * (page - 1) + 1));
            content.Add(new SqlParameter("@p1", size * page));
            string sql = @"select * from (
select *, ROW_NUMBER() OVER(ORDER BY Activetime DESC) as ROW
from animalrecord {0}
 ) FF
 WHERE FF.ROW BETWEEN  @p0 AND @p1";
            List<animalrecord> list = Query<animalrecord>(string.Format(sql, condition), content.ToArray());
            return list;
        }

        public int GetTotalSize(string roomid, string tableid)
        {
            string condition = "where 1=1 ";
            List<SqlParameter> content = new List<SqlParameter>();
            if (!string.IsNullOrEmpty(roomid))
            {
                condition += " and RoomID =@roomid";
                content.Add(new SqlParameter("@roomid", roomid));
            }
            if (!string.IsNullOrEmpty(tableid))
            {
                condition += " and TableID =@tableid";
                content.Add(new SqlParameter("@tableid", tableid));
            }
            string sql = @"select COUNT (RoomID) from 
(select  RoomID from animalrecord {0}  ) ff";
            return RunSQL(string.Format(sql, condition), content.ToArray());
        }
    }
}
